/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

//
// precursor.cpp (main.cpp is too crowded)
//

//
// includes
//
#include "main.h"
#include <QSqlQuery>
#include <QSqlError>

/*
================
addPrecursor
================
*/
void Main::addPrecursor( const QString &title, const QString &lot,
                         float baseAmount, Common::Units units,
                         bool solvent, float density ) {
    QSqlQuery query;

    // avoid duplicates
    if ( this->precursorForTitle( title ) != NULL )
        return;

    // perform database update and select last row    
    if ( !query.exec( QString( "insert into precursors values ( null, '%1', %2, '%3', '%4', '%5', '%6' )" )
                      .arg( title )                         // 1
                      .arg( density )                       // 2
                      .arg( baseAmount )                    // 3
                      .arg( static_cast<int>( units ))      // 4
                      .arg( lot )                           // 5
                      .arg( static_cast<int>( solvent ))    // 6
                      )) {
        this->error( StrSoftError + QString( "could not add precursor, reason: \"%1\"\n" ).arg( query.lastError().text()));
    }
    query.exec( QString( "select * from precursors where id=%1" ).arg( query.lastInsertId().toInt()));

    // get last entry and construct internal entry
    while ( query.next())
        this->precursorList << new PrecursorEntry( query.record(), "precursors" );
}

/*
================
removePrecursor
================
*/
void Main::removePrecursor( const QString &title ) {
    PrecursorEntry *pPtr = NULL;
    QSqlQuery query;

    // find precursor
    pPtr = this->precursorForTitle( title );

    // failsafe
    if ( pPtr == NULL )
        return;

    // remove precursor and logs from db
    query.exec( QString( "delete from precursors where id=%1" ).arg( pPtr->id()));
    //query.exec( QString( "delete from logs where precursorId=%1" ).arg( pPtr->id()));

    // remove from display
    this->precursorList.removeAll( pPtr );
}

/*
================
loadPrecursors
================
*/
void Main::loadPrecursors() {
    QSqlQuery query;

    // read stuff
    query.exec( "select * from precursors" );

    // store entries
    while ( query.next())
        this->precursorList << new PrecursorEntry( query.record(), "precursors" );

    // sort alphabetically
    //this->sort( Main::Precursors );
}

/*
================
precursorForId
================
*/
PrecursorEntry *Main::precursorForId( int id ) const {
    foreach ( PrecursorEntry *pPtr, this->precursorList ) {
        if ( pPtr->id() == id )
            return pPtr;
    }
    return NULL;
}

/*
================
precursorForTitle
================
*/
PrecursorEntry *Main::precursorForTitle( const QString &title ) const {
    foreach ( PrecursorEntry *pPtr, this->precursorList ) {
        if ( !QString::compare( title, pPtr->title()))
            return pPtr;
    }
    return NULL;
}
